import QtQuick 2.0
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.1
import org.qtproject.qtquick 1.0

import "qrc:/js/resources/Constants.js" as Constant
import "../common"
import "../configure"
import "../page"

MainBasePage {
    id: id_termPolicy

    width: 480 * m_ratio
    height: 640 * m_ratio

    m_header.title: "Term & Policy..."
    m_header.showBack: true;
    m_header.onBackClicked: {
        console.log("m_header.onBackClicked: ");
        id_pageView.pop();
    }


    anchors.leftMargin: m_margin
    anchors.rightMargin: m_margin
    anchors.topMargin: m_margin
    anchors.bottomMargin: m_margin

    property real m_ratio : ApplicationInfo.ratio
    property real progress: 0
    property int m_margin: 10 * m_ratio

    function notifyActiveCode(type) {
        console.log("TermAndPolicy.qml notifyActiveCode " + type)
        if (type === -1) {
            id_pageView.pop();
            id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/registration/ErrorNoneInternet.qml"));
        } else if (type === 0 || type === 3) {
            id_pageView.pop();
            id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/registration/ActiveCode.qml"));
        } else if (type === 1) {
            id_pageView.pop();
            id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/registration/ErrorPhoneNumber.qml"));
            //id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/registration/ActiveCode.qml"));
        }
    }

    RowLayout{
        anchors.top: m_header.bottom;

        ColumnLayout {
            id: id_comlumn
            spacing: 20* m_ratio
            RowLayout {
                spacing: 10 * m_ratio

                ListView {
                    id: id_termListView


                    width: id_termPolicy.width
                    height: id_termPolicy.height - id_accept.height - id_doCreateNewAccount.height - m_header.height - 40 * m_ratio
                    delegate: TermDelegate { }

                    model: TermModel { }
                }
            }

            ColumnLayout{

                RowLayout {
                    spacing: 10 * m_ratio
                    CheckBox {
                        id: id_checkAgreement
                        checked: true
                    }

                    Label {
                        id: id_accept
                        text: "I agree with the terms and policy above...!"

                    }
                }
                RowLayout {
                    ButtonDesigne {
                        id: id_doCreateNewAccount
                        text: "Agreement";
                        Layout.fillWidth: parent

                        enabled: id_checkAgreement.checked == true ? true : false

                        onClicked: {
                            ApplicationInfo.onGetActiveCodeRequest(Constant.m_phone);
                            console.log("TermAndPolicy.qml" + Constant.m_phone)
                            id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/registration/WaitingGetActiveCode.qml"));
                        }
                    }
                }
            }
        }
    }

    Component {
        id: id_touchStyle
        TextFieldStyle {
            //textColor: "white"
            //font.pixelSize: 28 *m_ratio
            background: Item {
                implicitHeight: 50 * m_ratio
                implicitWidth: 320 * m_ratio
                BorderImage {
                    source: "qrc:/staticdata/images/textinput.png"
                    border.left: 8 * m_ratio
                    border.right: 8 * m_ratio
                    anchors.bottom: parent.bottom
                    anchors.left: parent.left
                    anchors.right: parent.right
                }
            }
        }
    }


    Component {
        id: touchStyle
        ButtonStyle {
            panel: Item {
                implicitHeight: 50 * m_ratio
                implicitWidth: 320 * m_ratio
                BorderImage {
                    anchors.fill: parent
                    antialiasing: true
                    border.bottom: 8 * m_ratio
                    border.top: 8 * m_ratio
                    border.left: 8 * m_ratio
                    border.right: 8 * m_ratio
                    anchors.margins: control.pressed ? -4 : 0
                    source: control.pressed ? "qrc:/staticdata/images/button_pressed.png" : "qrc:/staticdata/images/button_default.png"
                    Text {
                        text: control.text
                        anchors.centerIn: parent
                        color: "white"
                        font.pixelSize: 23 * m_ratio
                        renderType: Text.NativeRendering
                    }
                }
            }
        }
    }

    Component.onCompleted:{
        ApplicationInfo.notifyActiveCode.connect(notifyActiveCode);
    }
}
